from django.contrib import admin
from finance_c2c.local_settings import Config
from apps.popular_img.models import PopularImg
from apps.popular_img.models import PasteInfo
from apps.popular_img.models import SalesManImg
from apps.popular_img.models import PasteInfoWords
from apps.popular_img.models import PopularCash
from apps.popular_img.models import SalesManPopular


class PopularInline(admin.TabularInline):
    model = PasteInfo
    extra = 1  # 用于添加外键关系模型的空行
    can_delete = True  # 是否显示删除框
    show_change_link = True


class PasteInfoWordsInline(admin.TabularInline):
    model = PasteInfoWords
    extra = 1  # 用于添加外键关系模型的空行
    can_delete = True  # 是否显示删除框
    show_change_link = True


class PopularImgAdmin(admin.ModelAdmin):
    inlines = [PopularInline, PasteInfoWordsInline]
    list_display = ("id", 'name')

    list_per_page = 50

    search_fields = ["name"]


class SalesManImgAdmin(admin.ModelAdmin):
    list_display = ("popular_img_name", 'salesman_name', "sales_img_url")

    def popular_img_name(self, model):
        return model.popular_img.name

    def salesman_name(self, model):
        return model.salesman.name

    def sales_img_url(self, model):
        return Config.no_protocol + model.sales_img

    popular_img_name.short_description = "推广图名称"
    salesman_name.short_description = "业务员"
    sales_img_url.short_description = "地址"


class PopularCashAdmin(admin.ModelAdmin):
    list_display = ("cash", "update_num", "update_time")


class SalesManPopularAdmin(admin.ModelAdmin):
    list_display = ("salesman_name",)

    def salesman_name(self, model):
        if model.salesman.name:
            return model.salesman.name
        else:
            return model.salesman.account


admin.site.register(PopularCash, PopularCashAdmin)
admin.site.register(PopularImg, PopularImgAdmin)
admin.site.register(SalesManImg, SalesManImgAdmin)
admin.site.register(SalesManPopular, SalesManPopularAdmin)
